package com.xrit.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xrit.constant.MessageConstant;
import com.xrit.exception.CustomException;
import com.xrit.mapper.MemberMapper;
import com.xrit.mapper.OrderMapper;
import com.xrit.mapper.SetMealMapper;
import com.xrit.pojo.Member;
import com.xrit.pojo.MemberExample;
import com.xrit.pojo.Order;
import com.xrit.pojo.OrderExample;
import com.xrit.query.QueryPageBean;
import com.xrit.result.PageResult;
import com.xrit.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 会员管理业务实现类
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Member findById(Integer id) {
        return memberMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setMealMapper.findSetmealCount();
    }

    @Override
    public List<Integer> getMemberReport(List<String> months) {
        List<Integer> count=new ArrayList<>();
        months.forEach(m->{
            Integer c = memberMapper.findMemberCountBeforeDate(m + ".01");
            count.add(c);
        });
        return count;
    }

    @Transactional
    @Override
    public Member login(String telephone) {
        //1.检查是否为会员
        MemberExample me=new MemberExample();
        MemberExample.Criteria mec = me.createCriteria();
        mec.andPhonenumberEqualTo(telephone);
        List<Member> list = memberMapper.selectByExample(me);
        if (list!=null && list.size()>0){
            //是，直接返回会员信息
            return list.get(0);
        }
        //不是，需要完成注册，并且获取会员id，将会员信息返回
        //TODO:新增添加会员的业务方法，直接调用，这里暂时还没写
        Member member=new Member();
        member.setPhonenumber(telephone);
        member.setRegtime(new Date());
        member.setRemark("手机快速注册");     //标记
        memberMapper.insertSelective(member);//完成会员注册
        return member;
    }

    @Override
    public int delete(Integer id) throws CustomException {
        //删除之前，检查此会员是否有预约体检套餐
        OrderExample oe=new OrderExample();
        OrderExample.Criteria oec = oe.createCriteria();
        oec.andMemberIdEqualTo(id);
        List<Order> list = orderMapper.selectByExample(oe);
        if (list!=null && list.size()>0){
            throw new CustomException(MessageConstant.MEMBER_IN_USE);
        }
        return memberMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(Member member) {
        return memberMapper.updateByPrimaryKeySelective(member);
    }

    @Override
    public int add(Member member) {
        member.setRegtime(new Date());
        return memberMapper.insertSelective(member);
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean){
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),true);
        Page<Member> page = memberMapper.selectByQueryString(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Member> findAll() {
        return memberMapper.selectByExample(null);
    }
}
